﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Db8.DataAccess;

namespace Db8.Controllers
{
    public class ControllerBase : Controller
    {
        public Data.User CurrentUser
        {
            get 
            {
                using (Db8Context db = new Db8Context())
                {
                    return db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
                }
            }
        }

        public string StickyFilter
        {
            get
            {
                return ClientSettings.StickyFilter;
            }
            set 
            {
                ClientSettings.StickyFilter = value;
            }
        }

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            if (User is Db8.Data.User)
                ViewBag.User = User as Db8.Data.User;
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            ViewBag.PageDescription = "A tool for cataloging common claims and responses to those claims. Sponsored by Democracy for America - Maricopo County";

            ViewData["NavHomeClass"] = "";
            ViewData["NavClaimClass"] = "";
            ViewData["NavAboutClass"] = "";
            ViewData["NavAdminClass"] = "";
            if (this is HomeController)
                ViewData["NavHomeClass"] = "selected";
            if (this is ClaimController)
                ViewData["NavClaimClass"] = "selected";
            if (this is AboutController)
                ViewData["NavAboutClass"] = "selected";
            if (this is AdminController)
                ViewData["NavAdminClass"] = "selected";

        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);

            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
            cache.SetCacheability(HttpCacheability.NoCache);
            cache.SetExpires(DateTime.Now.AddDays(-1));
        }
    }
}